package com.xiaoma.verbal.webservice;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoma.tpo.dao.PayOrderDao;
import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.service.UserService;
import com.xiaoma.tpo.utils.TPOUtils;
import com.xiaoma.verbal.dao.PayAudioGrabDao;
import com.xiaoma.verbal.dao.PayAudioMarktipDao;
import com.xiaoma.verbal.dao.PayAudioResultDao;
import com.xiaoma.verbal.domain.PayAudio;
import com.xiaoma.verbal.domain.PayAudioMarktip;
import com.xiaoma.verbal.domain.PayAudioResult;
import com.xiaoma.verbal.service.PayAudioService;

@Controller
@RequestMapping("/service/payAudio")
public class PayAudioWS {
    
    @Autowired
    private UserService userService;
   
    @Autowired
    private PayAudioService payAudioService;
    
    @Autowired
    private PayOrderDao payOrderDao;
    
    @Autowired
    private PayAudioGrabDao payAudioGrabDao;
    
    @Autowired
    private PayAudioResultDao payAudioResultDao;
    
    @Autowired
    private PayAudioMarktipDao payAudioMarktipDao;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> save(@RequestHeader String token, @RequestBody PayAudio audio) {
        Map<String, Object> result = new HashMap<String, Object>();
        User user = userService.findByToken(token);
        if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
            result.put("status", -1);
            result.put("message", "token disabled over 7 days, please login");
            return result;
        }
        Date now = new Date();
        audio.setUserId(user.getId());
        audio.setStatus(1);
        audio.setCreatedAt(now);
        audio.setUpdatedAt(now);
        payAudioService.save(audio);
        try {
            boolean b = payAudioService.post2Remote(audio, user);
            if (b) {
                audio.setStatus(2);
                audio.setUpdatedAt(new Date());
                payAudioService.update(audio);
                payOrderDao.finished(audio.getOrderNum());
                result.put("status", 1);
            } else {
                result.put("status", 0);
                result.put("message", "call xmpg api error!");
            }
        } catch(Exception e) {
            result.put("status", 0);
            result.put("message", e.getMessage());
        }
        userService.updateVisitTime(user);
        return result;
    }
    
    @RequestMapping(value = "/mine", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> mine(@RequestHeader String token) {
        Map<String, Object> result = new HashMap<String, Object>();
        User user = userService.findByToken(token);
        if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        result.put("payAudios", payAudioService.findByUserId(user.getId()));
        return result;
    }
    
    @RequestMapping(value = "/reSubmit/{payAudioId}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> mine(@PathVariable Integer payAudioId) {
        Map<String, Object> result = new HashMap<String, Object>();
        PayAudio audio = payAudioService.getById(payAudioId);
        if (audio != null && audio.getStatus() == 1) {//未提交成功
            User user = userService.findById(audio.getUserId());
            try {
                boolean b = payAudioService.post2Remote(audio, user);
                if (b) {
                    audio.setStatus(2);
                    audio.setUpdatedAt(new Date());
                    payAudioService.update(audio);
                    payOrderDao.finished(audio.getOrderNum());
                    result.put("status", 1);
                } else {
                    result.put("status", 0);
                    result.put("message", "call xmpg api error!");
                }
            } catch(Exception e) {
                result.put("status", 0);
                result.put("message", e.getMessage());
            }
        } else {
            result.put("status", 0);
            result.put("message", "invalid audio");
        }
        return result;
    }
    
    @RequestMapping(value = "/grab", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> grab(@RequestHeader String token) {
        Map<String, Object> result = new HashMap<String, Object>();
        User user = userService.findByToken(token);
        if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        result.put("records", payAudioGrabDao.findByUserId(user.getId()));
        userService.updateVisitTime(user);
        return result;
    }
    
    @RequestMapping(value = "/pgResult/{audioId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> pgResult(@PathVariable Integer audioId) {
        Map<String, Object> result = new HashMap<String, Object>();
        PayAudioResult entity1 = new PayAudioResult();
        entity1.setAudioId(audioId);
        List<PayAudioResult> results = payAudioResultDao.findByColumns(entity1);
        if (results.size() > 0) {
            PayAudioResult pgResult = results.get(0);
            result.put("pgResult", pgResult);
            PayAudioMarktip entity2 = new PayAudioMarktip();
            entity2.setAudioResultId(pgResult.getId());
            List<PayAudioMarktip> marktips = payAudioMarktipDao.findByColumns(entity2);
            result.put("marktips", marktips);
        } else {
            result.put("status", 0);
            result.put("message", "can't find with audioId " + audioId);
            
        }
        return result;
    }
}
